home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / BBEdit / MacBob 1.0ß2 / Examples / Format FTP Listing.bob < prev    next >
Encoding:
Text File  |  1995-12-12  |  2.1 KB  |  137 lines  |  [TEXT/R*ch]

  1. /***
  2.  *
  3.  *    Format FTP Listing.bob
  4.  *
  5.  *    Take an FTP directory listing (as produced by Anarchie)
  6.  *    and reformat it to a more legible form.
  7.  *
  8.  ***/
  9.  
  10. main ()
  11. {
  12.     tabSize = 4;        // Width of tab
  13.     width = 10;            // Width of file name column (x tabSize)
  14.     while (getfile("TEXT"))
  15.         Convert("");    // The string should be the modification date of the file
  16. }
  17.  
  18.  
  19. class List {
  20.     len, items;
  21.     Clear(); GetLen(); GetItems();
  22.     GetItem(n); Append(x);
  23. }
  24.  
  25. List::List (maxLen)
  26. {
  27.     len   = 0;
  28.     items = newvector(maxLen);
  29.     return this;
  30. }
  31.  
  32. List::Clear ()
  33. {
  34.     len = 0;
  35. }
  36.  
  37. List::GetLen ()
  38. {
  39.     return len;
  40. }
  41.  
  42. List::GetItems ()
  43. {
  44.     return items;
  45. }
  46.  
  47. List::GetItem (n)
  48. {
  49.     return items[n];
  50. }
  51.  
  52. List::Append (x)
  53. {
  54.     items[len++] = x;
  55. }
  56.  
  57.  
  58. Read_tab_delimited_line (list ; c, buf)
  59. {
  60.     if (typeof(list) == 0)        // if (list == nil)
  61.         list = new List(10);
  62.     else
  63.         list.Clear();
  64.     buf = "";
  65.  
  66.     while ((c = getc(stdin)) != -1) {
  67.         if (c == '\t' || c == '\n') {
  68.             list.Append(buf);
  69.             buf = "";
  70.             if (c == '\n')
  71.                 return list;
  72.         } else
  73.             buf += c;
  74.     }
  75.     return list;
  76. }
  77.  
  78.  
  79. StrEqual (a, b ; i)
  80. {
  81.     if (sizeof(a) != sizeof(b))
  82.         return 0;
  83.  
  84.     for (i = 0; i < sizeof(a); ++i)
  85.         if (a[i] != b[i])
  86.             return 0;
  87.     return 1;
  88. }
  89.  
  90.  
  91. SubStr (s, f, l ; len, r, i)
  92. {
  93.     len = sizeof(s) - f;
  94.     if (l > len)
  95.         l = len;
  96.     if (l <= 0) return "";
  97.     r = newstring(l);
  98.     for (i = 0; --l >= 0; )
  99.         r[i++] = s[f++];
  100.     return r;
  101. }
  102.  
  103.  
  104. Convert (cDate ; list, s, r, i, dirPre, filePre)
  105. {
  106.     list = Read_tab_delimited_line(nil);
  107.     s = list.GetItems();
  108.     r = s[list.GetLen() - 1];    // Last item in s
  109.  
  110.     for (i = sizeof(r) - 1; i >= 0; --i)
  111.         if (r[i] == '/')
  112.             break;
  113.  
  114.     dirPre  = "Δ\t";    // Directory prefix
  115.     filePre = "\t";        // File prefix
  116.  
  117.     d = StrEqual(s[0], "F") ? filePre : dirPre;
  118.  
  119.     print("ftp://", s[5], "/", SubStr(r, 0, i), "\t", cDate, "\n\n");
  120.     print(d, Pad(s[1], width), Pad(s[2], 2), s[3], "\n");
  121.     while (1) {
  122.         Read_tab_delimited_line(list);
  123.         if (list.GetLen() == 0)
  124.             break;
  125.         d = StrEqual(s[0], "F") ? filePre : dirPre;
  126.         print(d, Pad(s[1], width), Pad(s[2], 2), s[3], "\n");
  127.     }
  128.  
  129.     print("\n\n");
  130. }
  131.  
  132.  
  133. Pad (s, n)
  134. {
  135.     return s + SubStr("\t\t\t\t\t\t\t\t\t\t", 0, n - (sizeof(s) / tabSize));
  136. }
  137.